/* Copyright (c) 2010 OFXKit
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "OFXInvestmentStatementRequest.h"


@implementation OFXInvestmentStatementRequest

@synthesize includeOpenOrders;
@synthesize includePositions;
@synthesize dateOfPositions;
@synthesize includeInvestmentBalance;
@synthesize includeBankingTransactionImages;

- (id)init
{
  self = [super init];
  accountPrefix = @"INV";
  return self;
}

- (NSString*)formOFX1Request
{
  NSString* request = [super formOFX1Request];
  
  // remove the closing statement
  NSString* closingTag = [NSString stringWithFormat:@"</%@STMTRQ>", accountPrefix];
  NSRange start = [request rangeOfString:closingTag];
  request = [request substringToIndex:start.location];
  
  // add open orders
  NSString* incoo = @"N";
  if (includeOpenOrders) incoo = @"Y";
  request = [NSString stringWithFormat:@"%@<INCOO>%@</INCOO>", request, incoo];
  
  // add positions
  NSString* incpos = @"N";
  if (includePositions) incpos = @"Y";
  request = [NSString stringWithFormat:@"%@<INCPOS><DTASOF>%@</DTASOF><INCLUDE>%@</INCLUDE></INCPOS>",
             request, dateOfPositions, incpos];
  
  // add balance
  NSString* incbal = @"N";
  if (includeInvestmentBalance) incbal = @"Y";
  request = [NSString stringWithFormat:@"%@<INCBAL>%@</INCBAL></%@STMTRQ>", 
             request, incbal, accountPrefix];
    
  return request;
}

- (NSString*)formOFX2Request
{
  NSString* request = [super formOFX2Request];

  // remove the closing statement
  NSString* closingTag = [NSString stringWithFormat:@"</%@STMTRQ>", accountPrefix];
  NSRange start = [request rangeOfString:closingTag];
  request = [request substringToIndex:start.location];

  // add open orders
  NSString* incoo = @"N";
  if (includeOpenOrders) incoo = @"Y";
  request = [NSString stringWithFormat:@"%@<INCOO>%@</INCOO>", request, incoo];
  
  // add positions
  NSString* incpos = @"N";
  if (includePositions) incpos = @"Y";
  request = [NSString stringWithFormat:@"%@<INCPOS><DTASOF>%@</DTASOF><INCLUDE>%@</INCLUDE></INCPOS>",
             request, dateOfPositions, incpos];
  
  // add balance
  NSString* incbal = @"N";
  if (includeInvestmentBalance) incbal = @"Y";
  request = [NSString stringWithFormat:@"%@<INCBAL>%@</INCBAL>", request, incbal];

  // include banking transaction images?
  NSString* incimg = @"N";
  if (includeBankingTransactionImages) incimg = @"Y";
  request = [NSString stringWithFormat:@"%@<INCTRANIMG>%@</INCTRANIMG></%@STMTRQ>",
             request, incimg, accountPrefix];
  
  return request;
}

@end
